package cn.edu.nsmc.modules.sys.controller;

import cn.edu.nsmc.common.utils.R;
import cn.edu.nsmc.config.Constant;
import cn.edu.nsmc.modules.sys.entity.SysWorksheetTemplate;
import cn.edu.nsmc.modules.sys.form.BatchDeleteForm;
import cn.edu.nsmc.modules.sys.form.WorksheetTemplateList;
import cn.edu.nsmc.modules.sys.service.SysWorksheetTemplateService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/sys/worksheetTemplate")
public class SysWorksheetTemplateController {
    @Autowired
    SysWorksheetTemplateService sysWorksheetTemplateService;

    @PostMapping("/getWorksheetTemplateList")
    public R getWorksheetTemplateList(@RequestBody WorksheetTemplateList worksheetTemplateList) {
        return sysWorksheetTemplateService.getWorksheetTemplateList(worksheetTemplateList);
    }

    @GetMapping("/list")
    public R getList() {
        List<SysWorksheetTemplate> list = sysWorksheetTemplateService.getList();
        return R.ok().put("data",list);
    }

    @PostMapping("/addWorksheetTemplate")
    public R addWorksheetTemplate(@RequestBody SysWorksheetTemplate sysWorksheetTemplate) {
        return sysWorksheetTemplateService.addWorksheetTemplate(sysWorksheetTemplate);
    }

    @PutMapping("updateWorksheetTemplate")
    public R updatedWorksheetTemplate(@RequestBody SysWorksheetTemplate sysWorksheetTemplate) {
        return sysWorksheetTemplateService.updatedWorksheetTemplate(sysWorksheetTemplate);
    }

    // TODO: 2024/2/18 上次模板的接口
    @PutMapping("/deleteWorksheetTemplateByIds")
    public R deleteWorksheetTemplateByIds(@RequestParam("ids")  String[] ids) {
        return sysWorksheetTemplateService.deleteWorksheetTemplateByIds(ids);
    }

    @PostMapping("upload")
    public R fileUpload(@RequestParam("file") MultipartFile file){
        String path = Constant.WorksheetTemplateFilepath+"templates/";
        // 获取文件名(包括后缀)
        String fileName = file.getOriginalFilename();
        fileName=System.currentTimeMillis()%10000L+fileName;
        try (FileOutputStream fos = new FileOutputStream(path+fileName )) {
            fos.write(file.getBytes());
            return R.success("文件上传成功","templates/"+fileName);
        } catch (Exception e) {
            log.error("file upload failed, fileName = {}", file.getOriginalFilename(), e);
        }
        return R.error("文件上传失败");
    }
    // TODO: 2024/3/1 查询：返回文件流，1.预览，2.下载
    //http://localhost:8080/sys/

    @PostMapping("/onlinePreview/")
    public R onlinePreview(@Param("path") String path, HttpServletResponse response) throws Exception{
       return  sysWorksheetTemplateService.onlinePreview(path,response);
    }

    //下载模板文件
    // TODO: 2024/4/2 完善，数据库各字段。看看有没有问题
    @GetMapping("download")
    public void download(String filename, HttpServletResponse response) throws IOException {
        sysWorksheetTemplateService.download(filename,response);
    }

    @GetMapping("selectById/{id}")
    public R selectById(@PathVariable Long id){
        return sysWorksheetTemplateService.selectById(id);
    }

}
